<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    const kWeakestRows = (mat, k) => {
      let res = [];
      for (let i = 0; i < mat.length; i++) {
        // 查找每行中1的个数
        const num = mat[i].indexOf(0) === -1 ? mat[i].length : mat[i].indexOf(0);
        res.push([i, num]);
      }
      // 将res中的数组按照num进行升序排序
      res.sort((a, b) => a[1] - b[1]);
      // res中，提取num
      res = res.map(item => item[0]);
      // 截取前k个
      res.splice(k);
      return res;
    };
    console.log(kWeakestRows(mat =
      [[1, 1, 0, 0, 0],
      [1, 1, 1, 1, 0],
      [1, 0, 0, 0, 0],
      [1, 1, 0, 0, 0],
      [1, 1, 1, 1, 1]],
      k = 3));
  </script>
</body>

</html>